﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SaleSoft.Models;
using SaleSoft.Models.ScreenClient;
using SaleSoft.Constants;
using SaleSoft.Helper;
using Telerik.Web.Mvc;

namespace SaleSoft.Areas.administrator.Controllers
{
    public class LoaiTrangThaiController : BaseController {
        #region Properties Section
        private SaleSoftEntities db = new SaleSoftEntities();
        #endregion

        #region Views Section
        public ViewResult Index()
        {
            LoaiTrangThaiScreenClient screen = new LoaiTrangThaiScreenClient();
            if (Session[ScreenClientConstants.LOAI_TRANG_THAI_SCREENCLIENT] != null) {
                screen = (LoaiTrangThaiScreenClient)Session[ScreenClientConstants.LOAI_TRANG_THAI_SCREENCLIENT];
            }
            Search(screen);
            return View(screen);
        }

        [GridAction]
        public ActionResult _Index(LoaiTrangThaiScreenClient screen){
            Search(screen);
            return View(new GridModel(screen.DSLoaiTrangThai));
        }

        #endregion

        #region CRUD Section

        [HttpPost]
        [GridAction]
        public ActionResult _Insert () {
            SaleSoft.Models.BaseClient.LoaiTrangThai loaiClient = new Models.BaseClient.LoaiTrangThai();
            TryUpdateModel(loaiClient);

            loai_trangthai loai = new loai_trangthai() { 
                tenloaitrangthai = loaiClient.tenloaitrangthai
            };

            db.loai_trangthai.AddObject(loai);
            db.SaveChanges();

            LoaiTrangThaiScreenClient screen = new LoaiTrangThaiScreenClient();
            if (Session[ScreenClientConstants.LOAI_TRANG_THAI_SCREENCLIENT] != null) {
                screen = (LoaiTrangThaiScreenClient)Session[ScreenClientConstants.LOAI_TRANG_THAI_SCREENCLIENT];
            }
            Search(screen);
            return View(new GridModel(screen.DSLoaiTrangThai));
        }

        [HttpPost]
        [GridAction]
        public ActionResult _Edit ( int id ) {
            SaleSoft.Models.BaseClient.LoaiTrangThai loaiClient = new Models.BaseClient.LoaiTrangThai();
            TryUpdateModel(loaiClient);
            loaiClient.id_loaitrangthai = id;
            loai_trangthai loai = db.loai_trangthai.Single(p => p.id_loaitrangthai == id);
            ObjectHelper.Copy(loaiClient, loai);

            db.ObjectStateManager.ChangeObjectState(loai, EntityState.Modified);

            LoaiTrangThaiScreenClient screen = new LoaiTrangThaiScreenClient();
            if (Session[ScreenClientConstants.LOAI_TRANG_THAI_SCREENCLIENT] != null) {
                screen = (LoaiTrangThaiScreenClient)Session[ScreenClientConstants.LOAI_TRANG_THAI_SCREENCLIENT];
            }
            Search(screen);
            return View(new GridModel(screen.DSLoaiTrangThai));
        }

        [HttpPost]
        [GridAction]
        public ActionResult _Delete ( int id ) {
            loai_trangthai loai = db.loai_trangthai.Single(p => p.id_loaitrangthai == id);
            if (loai.ds_trangthai.Count == 0) {
                db.loai_trangthai.DeleteObject(loai);
                db.SaveChanges();
            }
            LoaiTrangThaiScreenClient screen = new LoaiTrangThaiScreenClient();
            if (Session[ScreenClientConstants.LOAI_TRANG_THAI_SCREENCLIENT] != null) {
                screen = (LoaiTrangThaiScreenClient)Session[ScreenClientConstants.LOAI_TRANG_THAI_SCREENCLIENT];
            }
            Search(screen);
            return View(new GridModel(screen.DSLoaiTrangThai));
        }
        #endregion

        #region Helper Section
        private void Search ( LoaiTrangThaiScreenClient screen ) {
            var data = db.loai_trangthai.OrderBy(o => o.tenloaitrangthai);
            screen.DSLoaiTrangThai.Clear();
            foreach (loai_trangthai loai in data) {
                SaleSoft.Models.BaseClient.LoaiTrangThai loaiClient = new Models.BaseClient.LoaiTrangThai();
                ObjectHelper.Copy(loai, loaiClient);
                loaiClient.Deleteable = loai.ds_trangthai.Count == 0;
                screen.DSLoaiTrangThai.Add(loaiClient);
            }
            Session[ScreenClientConstants.LOAI_TRANG_THAI_SCREENCLIENT] = screen;
        }
        #endregion

        #region Additional Section
        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }

        #endregion
    }
}